```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Bean作用域详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        }
        .code-block {
            background-color: #f8f9fa;
            border-left: 4px solid #4facfe;
        }
        .scope-card {
            transition: all 0.3s ease;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .scope-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .scope-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
            margin-bottom: 1rem;
        }
        .dropdown-content {
            display: none;
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        .dropdown:hover .dropdown-content {
            display: block;
            opacity: 1;
        }
        .mermaid {
            background-color: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6">Spring Bean 作用域深度解析</h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">掌握Spring框架中Bean的生命周期与可见性范围</p>
                <div class="flex justify-center space-x-4">
                    <a href="#scopes" class="px-6 py-3 bg-white text-blue-600 font-semibold rounded-lg hover:bg-gray-100 transition duration-300">
                        <i class="fas fa-layer-group mr-2"></i>查看作用域
                    </a>
                    <a href="#comparison" class="px-6 py-3 bg-transparent border-2 border-white text-white font-semibold rounded-lg hover:bg-white hover:text-blue-600 transition duration-300">
                        <i class="fas fa-table mr-2"></i>对比分析
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Breadcrumb -->
    <div class="container mx-auto px-6 py-4">
        <nav class="flex" aria-label="Breadcrumb">
            <ol class="inline-flex items-center space-x-1 md:space-x-3">
                <li class="inline-flex items-center">
                    <a href="#" class="inline-flex items-center text-sm font-medium text-gray-600 hover:text-blue-600">
                        <i class="fas fa-home mr-2"></i>
                        首页
                    </a>
                </li>
                <li>
                    <div class="flex items-center">
                        <i class="fas fa-chevron-right text-gray-400"></i>
                        <a href="#" class="ml-1 text-sm font-medium text-gray-600 hover:text-blue-600 md:ml-2">Spring框架</a>
                    </div>
                </li>
                <li aria-current="page">
                    <div class="flex items-center">
                        <i class="fas fa-chevron-right text-gray-400"></i>
                        <span class="ml-1 text-sm font-medium text-blue-600 md:ml-2">Bean作用域</span>
                    </div>
                </li>
            </ol>
        </nav>
    </div>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="max-w-3xl mx-auto">
                <p class="text-lg text-gray-700 mb-6 leading-relaxed">
                    在Spring框架中，Bean的作用域定义了Bean实例的生命周期和可见性范围。正确选择作用域对于构建高效、可靠的应用程序至关重要。本文将详细介绍Spring提供的五种主要作用域，帮助您根据具体需求做出明智的选择。
                </p>
                <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-8">
                    <div class="flex">
                        <div class="flex-shrink-0 text-blue-500">
                            <i class="fas fa-info-circle text-xl"></i>
                        </div>
                        <div class="ml-3">
                            <p class="text-sm text-blue-700">
                                <strong>专业提示：</strong> 默认情况下，Spring的Bean作用域是Singleton，适用于大部分情况。根据需要选择合适的生命周期管理策略。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- All Scopes Section -->
        <section id="scopes" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-center">Spring Bean作用域类型</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
                <!-- Singleton -->
                <div class="scope-card bg-white p-6">
                    <div class="scope-icon bg-blue-100 text-blue-600">
                        <i class="fas fa-cube text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Singleton（单例）</h3>
                    <p class="text-gray-600 mb-4">在整个应用程序中只存在一个Bean实例。每次请求该Bean时，都会返回同一个实例。</p>
                    <div class="code-block p-4 mb-4 rounded">
                        <pre><code class="text-sm">@Component
@Scope("singleton")
public class ExampleBean {
    // Bean属性和方法
}</code></pre>
                    </div>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-info-circle mr-2"></i>
                        <span>默认作用域，适用于无状态服务</span>
                    </div>
                </div>

                <!-- Prototype -->
                <div class="scope-card bg-white p-6">
                    <div class="scope-icon bg-green-100 text-green-600">
                        <i class="fas fa-clone text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Prototype（原型）</h3>
                    <p class="text-gray-600 mb-4">每次请求Bean时，都会创建一个新的实例。IOC容器不会管理其生命周期。</p>
                    <div class="code-block p-4 mb-4 rounded">
                        <pre><code class="text-sm">@Component
@Scope("prototype")
public class ExampleBean {
    // Bean属性和方法
}</code></pre>
                    </div>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-info-circle mr-2"></i>
                        <span>适用于有状态对象，每次需要新实例</span>
                    </div>
                </div>

                <!-- Request -->
                <div class="scope-card bg-white p-6">
                    <div class="scope-icon bg-purple-100 text-purple-600">
                        <i class="fas fa-globe text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Request（请求）</h3>
                    <p class="text-gray-600 mb-4">在每个HTTP请求中创建一个新的Bean实例。仅适用于Web应用程序。</p>
                    <div class="code-block p-4 mb-4 rounded">
                        <pre><code class="text-sm">@Component
@Scope("request")
public class ExampleBean {
    // Bean属性和方法
}</code></pre>
                    </div>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-info-circle mr-2"></i>
                        <span>适用于Web请求相关的数据</span>
                    </div>
                </div>

                <!-- Session -->
                <div class="scope-card bg-white p-6">
                    <div class="scope-icon bg-yellow-100 text-yellow-600">
                        <i class="fas fa-user-clock text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Session（会话）</h3>
                    <p class="text-gray-600 mb-4">在每个HTTP会话中创建一个新的Bean实例。仅适用于Web应用程序。</p>
                    <div class="code-block p-4 mb-4 rounded">
                        <pre><code class="text-sm">@Component
@Scope("session")
public class ExampleBean {
    // Bean属性和方法
}</code></pre>
                    </div>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-info-circle mr-2"></i>
                        <span>适用于用户会话相关的数据</span>
                    </div>
                </div>

                <!-- Global Session -->
                <div class="scope-card bg-white p-6">
                    <div class="scope-icon bg-red-100 text-red-600">
                        <i class="fas fa-users text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Global Session（全局会话）</h3>
                    <p class="text-gray-600 mb-4">在全局HTTP会话中创建一个新的Bean实例。仅适用于Portlet Web应用程序。</p>
                    <div class="code-block p-4 mb-4 rounded">
                        <pre><code class="text-sm">@Component
@Scope("globalSession")
public class ExampleBean {
    // Bean属性和方法
}</code></pre>
                    </div>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-info-circle mr-2"></i>
                        <span>Portlet应用中的全局用户会话</span>
                    </div>
                </div>

                <!-- Custom Scope -->
                <div class="scope-card bg-white p-6">
                    <div class="scope-icon bg-indigo-100 text-indigo-600">
                        <i class="fas fa-cog text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">自定义作用域</h3>
                    <p class="text-gray-600 mb-4">Spring允许定义自定义的作用域，满足特殊业务需求。</p>
                    <div class="code-block p-4 mb-4 rounded">
                        <pre><code class="text-sm">public class CustomScope implements Scope {
    // 实现自定义作用域逻辑
}</code></pre>
                    </div>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-info-circle mr-2"></i>
                        <span>用于特殊生命周期管理需求</span>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section id="comparison" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-center">作用域对比分析</h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-12">
                <div class="mermaid">
                    graph TD
                    A[Bean作用域] --> B[Singleton]
                    A --> C[Prototype]
                    A --> D[Web作用域]
                    D --> E[Request]
                    D --> F[Session]
                    D --> G[GlobalSession]
                    style A fill:#4facfe,color:#fff
                    style B fill:#e3f2fd,color:#333
                    style C fill:#e3f2fd,color:#333
                    style D fill:#bbdefb,color:#333
                    style E fill:#f5f5f5,color:#333
                    style F fill:#f5f5f5,color:#333
                    style G fill:#f5f5f5,color:#333
                </div>
            </div>

            <div class="overflow-x-auto">
                <table class="min-w-full bg-white rounded-lg overflow-hidden">
                    <thead class="bg-gray-100">
                        <tr>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">作用域</th>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">生命周期</th>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">适用场景</th>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">线程安全</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <tr>
                            <td class="py-3 px-4 font-medium">Singleton</td>
                            <td class="py-3 px-4">整个应用生命周期</td>
                            <td class="py-3 px-4">无状态服务、工具类</td>
                            <td class="py-3 px-4">需注意线程安全</td>
                        </tr>
                        <tr class="bg-gray-50">
                            <td class="py-3 px-4 font-medium">Prototype</td>
                            <td class="py-3 px-4">每次请求新实例</td>
                            <td class="py-3 px-4">有状态对象</td>
                            <td class="py-3 px-4">通常线程安全</td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4 font-medium">Request</td>
                            <td class="py-3 px-4">HTTP请求期间</td>
                            <td class="py-3 px-4">请求相关数据</td>
                            <td class="py-3 px-4">线程安全</td>
                        </tr>
                        <tr class="bg-gray-50">
                            <td class="py-3 px-4 font-medium">Session</td>
                            <td class="py-3 px-4">用户会话期间</td>
                            <td class="py-3 px-4">用户相关数据</td>
                            <td class="py-3 px-4">线程安全</td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4 font-medium">GlobalSession</td>
                            <td class="py-3 px-4">全局会话期间</td>
                            <td class="py-3 px-4">Portlet应用</td>
                            <td class="py-3 px-4">线程安全</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>

        <!-- Best Practices -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center">最佳实践与建议</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-blue-100 p-2 rounded-lg mr-4">
                            <i class="fas fa-check-circle text-blue-600 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold">Singleton使用原则</h3>
                    </div>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2 text-sm"></i>
                            <span>优先考虑Singleton作用域，减少对象创建开销</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2 text-sm"></i>
                            <span>确保Singleton Bean是无状态的或做好线程安全处理</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2 text-sm"></i>
                            <span>避免在Singleton Bean中保存请求或会话状态</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-green-100 p-2 rounded-lg mr-4">
                            <i class="fas fa-exclamation-triangle text-green-600 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold">作用域选择注意事项</h3>
                    </div>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2 text-sm"></i>
                            <span>Prototype作用域会增加GC压力，避免滥用</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2 text-sm"></i>
                            <span>Web作用域Bean不能在非Web环境中使用</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2 text-sm"></i>
                            <span>测试时注意作用域的影响，特别是Prototype和Web作用域</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- FAQ Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center">常见问题解答</h2>
            
            <div class="space-y-4">
                <div class="dropdown group">
                    <button class="flex items-center justify-between w-full px-6 py-4 bg-white rounded-lg shadow-sm text-left hover:bg-gray-50 transition duration-200">
                        <span class="font-semibold">如何判断应该使用Singleton还是Prototype作用域？</span>
                        <i class="fas fa-chevron-down text-gray-400 transition-transform duration-200 group-hover:rotate-180"></i>
                    </button>
                    <div class="dropdown-content px-6 pt-2 pb-4 bg-gray-50 rounded-b-lg">
                        <p class="text-gray-700">
                            判断标准主要基于Bean的状态特性。如果Bean是无状态的（不保存特定于请求或用户的临时数据），应该使用Singleton作用域。如果Bean需要在每次使用时保持独立状态（如包含请求特定数据的对象），则应该使用Prototype作用域。简单记忆法：无状态用Singleton，有状态用Prototype。
                        </p>
                    </div>
                </div>
                
                <div class="dropdown group">
                    <button class="flex items-center justify-between w-full px-6 py-4 bg-white rounded-lg shadow-sm text-left hover:bg-gray-50 transition duration-200">
                        <span class="font-semibold">Web作用域Bean在非Web环境下会怎样？</span>
                        <i class="fas fa-chevron-down text-gray-400 transition-transform duration-200 group-hover:rotate-180"></i>
                    </button>
                    <div class="dropdown-content px-6 pt-2 pb-4 bg-gray-50 rounded-b-lg">
                        <p class="text-gray-700">
                            如果在非Web环境（如单元测试、命令行应用）中使用Request或Session作用域的Bean，Spring会抛出IllegalStateException异常。解决方案有：1) 修改Bean为适合非Web环境的作用域；2) 在测试中模拟Web环境（如使用MockHttpServletRequest）；3) 为不同环境配置不同的Bean定义。
                        </p>
                    </div>
                </div>
                
                <div class="dropdown group">
                    <button class="flex items-center justify-between w-full px-6 py-4 bg-white rounded-lg shadow-sm text-left hover:bg-gray-50 transition duration-200">
                        <span class="font-semibold">Singleton作用域Bean如何安全地引用Prototype作用域Bean？</span>
                        <i class="fas fa-chevron-down text-gray-400 transition-transform duration-200 group-hover:rotate-180"></i>
                    </button>
                    <div class="dropdown-content px-6 pt-2 pb-4 bg-gray-50 rounded-b-lg">
                        <p class="text-gray-700">
                            直接注入会导致Prototype Bean实际上也变成Singleton行为。解决方案有：1) 使用方法注入（@Lookup）；2) 使用ObjectProvider（Spring 4.3+）；3) 通过ApplicationContext直接获取Bean；4) 使用代理模式（@Scope(proxyMode=...)）。方法2是最推荐的现代解决方案。
                        </p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <h3 class="text-xl font-semibold text-white mb-2">技术小馆</h3>
                <p class="mb-6">专注于IT技术的知识分享平台</p>
                <div class="flex justify-center space-x-4">
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-white transition duration-300">
                        <i class="fas fa-globe mr-1"></i> www.yuque.com/jtostring
                    </a>
                </div>
                <div class="mt-8 pt-6 border-t border-gray-800 text-sm text-gray-500">
                    &copy; 2023 技术小馆. 保留所有权利.
                </div>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
        
        // Dropdown functionality
        document.querySelectorAll('.dropdown').forEach(dropdown => {
            const button = dropdown.querySelector('button');
            const content = dropdown.querySelector('.dropdown-content');
            
            button.addEventListener('click', () => {
                const isOpen = content.style.display === 'block';
                content.style.display = isOpen ? 'none' : 'block';
                setTimeout(() => {
                    content.style.opacity = isOpen ? '0' : '1';
                }, 10);
            });
        });
    </script>
</body>
</html>
```